# -*- coding: utf-8 -*-
"""
@Time : 2023/7/28 16:34
@Author : ChenXiaoliang
@Email : middlegod@sina.com
@File : add_borrower.py
@Desc : 增加人员信息
"""
import sys
import datetime
from flask_restful import Resource, reqparse

from service.mysql_connect import MySQLConnected
from service.config_import import load_yaml

sys.path.append('..')

parser = reqparse.RequestParser()
parser.add_argument('person_name', type=str, help="参数person_name不能为空", required=True)


class AddBorrower(Resource):
    def post(self):
        args = parser.parse_args()
        person_name = args.get('person_name').strip()
        yaml_config_path = "config_info.yaml"
        params = load_yaml(yaml_config_path)
        dbinfo = params['databases']['library_db_info']
        db = params['databases']['library_db_name']
        connected = MySQLConnected(dbinfo)
        # print(connected.con)
        # print(connected.cur)
        # print("insert into test_roast.roast (val) values ('" + roast + "');")
        # 数据库名：test_library 数据表名：borrowers
        # 数据库DDL
        # CREATE TABLE `borrowers`(
        #     `id` int NOT NULL AUTO_INCREMENT,
        #     `name` varchar(150) NOT NULL COMMENT '人员名称',
        #     `valid` int NOT NULL DEFAULT '1' COMMENT '人员状态,1-在职,0-离职',
        #     `creation` varchar(50) DEFAULT NULL COMMENT '创建时间,人员录入到数据库的时间',
        #     PRIMARY KEY (`id`)
        # ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
        now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        sql = "insert into " + db + ".borrowers(name,valid,creation) values (%s, 1," + now + ");"
        connected.cur.execute(sql, person_name)
        # connected.cur.execute(("insert into " + db + ".roast (val, ts) values ('" + roast + "',%s);"), now)
        # 提交到数据库
        connected.con.commit()
        # 关闭游标
        connected.cur.close()
        # 关闭数据库
        connected.con.close()

        return {'content': person_name+"添加成功~", 'result': 'success'}
